package com.server.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.server.model.OsaCashBackOrder;
import com.server.model.OsaUserInfo;
import com.server.service.IOsaCashBackOrderService;
import com.server.service.IOsaUserInfoService;
import com.server.utils.UUIDUtil;

@RestController
public class OsaController {
	
	private Logger logger = Logger.getLogger(OsaController.class);
	
	@Autowired
	private IOsaUserInfoService iOsaUserInfoService;
	@Autowired
	private IOsaCashBackOrderService iOsaCashBackOrderService;
	
	@RequestMapping(value = {"/","/index"})
    public Object index() {
		Result result = new Result();
		result.setMsg("Welcome to Osa Api!");
		result.setCode(Result.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/user")
    public Object user() {
		Result result = new Result();
		OsaUserInfo osaUserInfo = new OsaUserInfo();
		osaUserInfo.setUserId(2l);
		result.setData(iOsaUserInfoService.find(osaUserInfo));
		result.setMsg("Welcome to Osa Api!");
		result.setCode(Result.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/checkUser")
    public Object checkUser(@RequestParam String weChat) { // 查询用户是否已经注册
		logger.info("请求的用户微信昵称------>" + weChat);
		Result result = new Result();
		OsaUserInfo osaUserInfo = new OsaUserInfo();
		osaUserInfo.setWechat(weChat);
		List<OsaUserInfo> lists = iOsaUserInfoService.find(osaUserInfo);
		if (lists != null && lists.size() > 0) {
			result.setMsg("用户已经注册");
			result.setCode(Result.SUCCESS);
		} else {
			result.setMsg("此用户未注册");
			result.setCode(Result.FAIL);
		}
		return result;
	}
	
	@RequestMapping(value = "/registerUser")
    public Object registerUser(OsaUserInfo osaUserInfo) { // 注册用户信息
		Result result = new Result();
		List<OsaUserInfo> lists = iOsaUserInfoService.find(osaUserInfo);
		if (lists == null) {
			osaUserInfo.setUserCode(UUIDUtil.getUuid());
			Long id = iOsaUserInfoService.insert(osaUserInfo);
			result.setData(id);
			result.setMsg("成功");
			result.setCode(Result.SUCCESS);
		} else {
			result.setMsg("用户已经注册过");
			result.setCode(Result.FAIL);
		}
		return result;
	}
	
	@RequestMapping(value = "/findUser")
    public Object findUser(OsaUserInfo osaUserInfo) { // 查询用户信息
		Result result = new Result();
		List<OsaUserInfo> lists = iOsaUserInfoService.find(osaUserInfo);
		if (lists != null && lists.size() > 0) {
			result.setData(lists.get(0));
			result.setMsg("成功");
			result.setCode(Result.SUCCESS);
		} else {
			result.setMsg("用户不存在");
			result.setCode(Result.FAIL);
		}
		return result;
	}
	
	@RequestMapping(value = "/recordOrder")
    public Object recordOrder(OsaCashBackOrder osaCashBackOrder) { // 录入订单信息
		Result result = new Result();
		List<OsaCashBackOrder> lists = iOsaCashBackOrderService.find(osaCashBackOrder);
		if (lists == null) {
			int id = iOsaCashBackOrderService.insert(osaCashBackOrder);
			result.setData(id);
			result.setMsg("成功");
			result.setCode(Result.SUCCESS);
		} else {
			result.setMsg("订单已存在");
			result.setCode(Result.FAIL);
		}
		return result;
	}
	
	@RequestMapping(value = "/orderList")
    public Object orderList(OsaCashBackOrder osaCashBackOrder) { // 查询订单信息
		Result result = new Result();
		List<OsaCashBackOrder> lists = iOsaCashBackOrderService.find(osaCashBackOrder);
		if (lists != null && lists.size() > 0) {
			result.setData(lists);
			result.setMsg("成功");
			result.setCode(Result.SUCCESS);
		} else {
			result.setMsg("未录入订单");
			result.setCode(Result.FAIL);
		}
		return result;
	}

	class Result {
		private static final String SUCCESS = "1";
		private static final String FAIL = "0";
		private String code;
		private String msg;
		private Object data;
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public Object getData() {
			return data;
		}
		public void setData(Object data) {
			this.data = data;
		}
	}
}
